home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / binaries / Windows / jsdk / src / sun / servlet / isapi / ISAPIConnectionHandler.java < prev    next >
Encoding:
Java Source  |  1997-07-18  |  4.5 KB  |  195 lines

  1. /*
  2.  * @(#)ISAPIConnectionHandler.java    1.7 97/07/16
  3.  * 
  4.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.0
  20.  */
  21.  
  22. package sun.servlet.isapi;
  23.  
  24. import javax.servlet.*;
  25. import java.net.*;
  26. import java.io.*;
  27. import sun.servlet.ServletConnection;
  28. import sun.servlet.http.*;
  29.  
  30. /**
  31.  * This class represents a servlet connection handler in the servlet server.
  32.  *
  33.  * @version    1.7, 07/16/97
  34.  * @author    Jongyoon Lee
  35.  */
  36. class ISAPIConnectionHandler implements ServletConnection {
  37.     /**
  38.      * The http version number.
  39.      */
  40.     public static final String HTTP_VERSION = "HTTP/1.0";
  41.  
  42.     /**
  43.      * The server for this handler.
  44.      */
  45.     protected ServletServer server;
  46.  
  47.     /**
  48.      * The servlet request.
  49.      */
  50.     protected final ISAPIRequest req = new ISAPIRequest();
  51.  
  52.     /**
  53.      * The servlet response.
  54.      */
  55.     protected final ISAPIResponse res = new ISAPIResponse();
  56.  
  57.     /**
  58.      * The current ISAPI connection.
  59.      */
  60.     protected ISAPIConnection conn;
  61.  
  62.     /**
  63.      * Creates a new handler for this specified server.
  64.      */
  65.     protected ISAPIConnectionHandler(ServletServer server) {
  66.     this.server = server;
  67.     conn = new ISAPIConnection();
  68.     }
  69.  
  70.     /**
  71.      * Runs the connection handler
  72.      */
  73.     public void run() {
  74.     handleConnection(conn);
  75.     }
  76.  
  77.     void init(long ecb) {
  78.     conn.reset();
  79.     conn.init(ecb);
  80.     }
  81.  
  82.     /**
  83.      * Handles a single connection from the client.
  84.      * @param conn the ISAPI connection
  85.      */
  86.     protected void handleConnection(ISAPIConnection conn) {
  87.     req.init(conn);
  88.     res.init(conn);
  89.     try {
  90.         //res.setProtocol(HTTP_VERSION);
  91.         //res.setHeader("Server", req.getServerName());
  92.         // no keep-alive for now
  93.         res.setKeepAlive(false);
  94.         sendResponse(req, res);
  95.     } catch (Throwable e) {
  96.         if (res.getTotalBytes() == 0) {
  97.         try {
  98.             res.sendError(res.SC_INTERNAL_SERVER_ERROR);
  99.         } catch (IOException ee) {
  100.             // eat it!
  101.         }
  102.         }
  103.     } finally {
  104.         try {
  105.         res.finish();
  106.         } catch (IOException ee) {
  107.         // eat it!
  108.         }
  109.     }
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Sends a response to the client.
  115.      * @ServletException thrown if the servlet throws an exception in service.
  116.      */
  117.     protected void sendResponse(ISAPIRequest req, ISAPIResponse res)
  118.     throws ServletException, IOException
  119.     {
  120.     String name = req.getServletPath();
  121.     if (name != null) {
  122.         // invoke servlet
  123.         name = name.substring(1);
  124.         int index;
  125.         if ((index = name.indexOf('/')) != -1) {
  126.         name = name.substring(index + 1);
  127.         }
  128.         if ((index = name.indexOf('/')) != -1) {
  129.         name = name.substring(0, index);
  130.         }
  131.         Servlet s = server.getServlet(name);
  132.         if (s != null) {
  133.         s.service(req, res);
  134.         return;
  135.         }
  136.     }
  137.     res.sendError(res.SC_NOT_FOUND);
  138.     }
  139.  
  140.     // ServletConnection interface
  141.  
  142.     /**
  143.      * Returns the host name of the server that received the request.
  144.      */  
  145.     public String getServerName() {
  146.     return req.getServerName();
  147.     }
  148.  
  149.     /**
  150.      * Returns the port number on which this request was received.
  151.      */  
  152.     public int getServerPort() {
  153.     return req.getServerPort();
  154.     }
  155.  
  156.     /**  
  157.      * Returns the fully qualified host name of the agent that sent the
  158.      * request.
  159.      */  
  160.     public String getRemoteHost() {
  161.     return req.getRemoteHost();
  162.     }
  163.  
  164.     /**
  165.      * Returns the IP address of the agent that sent the request.
  166.      */
  167.     public String getRemoteAddr() {
  168.     return req.getRemoteAddr();
  169.     }
  170.  
  171.     /**
  172.      * Returns the specified path translated to a real path.
  173.      */
  174.     public String getRealPath(String path) {
  175.     return req.getRealPath(path);
  176.     }
  177.  
  178.     /**
  179.      * Returns an input stream for reading from the connection.
  180.      */
  181.     public InputStream getInputStream() throws IOException {
  182.     return req.getInputStream();
  183.     }
  184.  
  185.     /**
  186.      * Returns an output stream for writing to the connection.
  187.      */
  188.     public OutputStream getOutputStream() throws IOException {
  189.     return res.getOutputStream();
  190.     }
  191. }
  192.  
  193.  
  194.  
  195.